package com.android.loveseason;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

public class LoveSeanProvider extends ContentProvider implements BaseColumns{
	public static final Uri PLAYERINFO_URI = Uri.parse("content://LoveSeanProvider/player");
	public static final Uri PEERINFO_URI = Uri.parse("content://LoveSeanProvider/peer");
	public static final Uri PLAYERINFO_STATE_URI = Uri.parse("content://LoveSeanProvider/player_state");
	
	private static int PLAYER_ID = 1;
	private static int PEER_ID = 2;
	private static int PLAYER_STATE=3;
	
	private static final UriMatcher URL_MATCHER =
            new UriMatcher(UriMatcher.NO_MATCH);
	static {
        URL_MATCHER.addURI("LoveSeanProvider", "player", PLAYER_ID);
        URL_MATCHER.addURI("LoveSeanProvider", "peer", PEER_ID);
        URL_MATCHER.addURI("LoveSeanProvider", "player_state", PLAYER_STATE);
    }
	///loveseason_PlayerProvider
	///loveseason_PeerProvider
	private String TAG = "LoveSeanProvider";
	private String mdb_name = "loveseason.mdb";
	private String mtable_player = "player_tab";
	private String mtable_player_state = "player_state";
	private String mtable_peer = "peer_tab";
	
	public static final String Column_ID = "user_id";
	public static final String Column_name = "user_name";
	public static final String Column_jinder = "user_jinder";
	public static final String Column_blood = "user_blood";
	public static final String Column_birth = "user_birth";
	public static final String Column_headId = "user_headid";
	private static final String Column_peerId = "peer_id";
	
	public static final String Column_knowledge = "knowledge_value";
	public static final String Column_Hp = "health_value";
	public static final String Column_feel = "feel_value";
	public static final String Column_Money = "money_value";
	public static final String Column_Date = "date_value";
	
	private  String CreateTable_player = "create table "+mtable_player+" ("+Column_ID +" integer primary key autoincrement, "+
	Column_name+" text  not null,"+Column_jinder+" integer not null,"+Column_blood+" integer not null,"+Column_birth+" text not null,"+
	Column_headId+" integer not null)";
	
	private  String CreateTable_peer = "create table "+mtable_peer+" ("+Column_peerId +" integer primary key autoincrement, "+
			Column_name+" text  not null,"+Column_jinder+" integer not null,"+Column_blood+" integer not null,"+Column_birth+" text not null,"+
			Column_headId+" integer not null,"+Column_ID+" integer not null)";
	
	private String CreateTable_playerstate = "create table "+mtable_player_state+" ("+Column_ID +" integer not null, "+
			Column_Hp+" integer  not null,"+Column_feel+" integer not null,"+Column_Money+" integer not null,"+Column_knowledge+" integer not null,"+
			Column_Date+" text not null)";
	
	private DatabaseHelper mdb_helper = null;
	private SQLiteDatabase gamemdb = null;
	
	private  class DatabaseHelper extends SQLiteOpenHelper {
		
		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version); 
			
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.i(TAG,"Create table");
			//create table peer
			db.execSQL(CreateTable_peer);
			//create table player
			db.execSQL(CreateTable_player); 
			
			//create table player state
			db.execSQL(CreateTable_playerstate);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}
		
		
	};

	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Uri insert(Uri uri, ContentValues value) {
		Log.i(TAG,"insert the info");
		if(PLAYER_ID == URL_MATCHER.match(uri)){

			        String InsertPlayerStr = String.format("insert into %s(%s, %s, %s, %s, %s) values('%s', %d, %d, '%s', '%d')", 
					mtable_player,Column_name,Column_jinder,Column_blood,Column_birth,Column_headId,
					value.getAsString(Column_name),value.getAsInteger(Column_jinder),value.getAsInteger(Column_blood),
					value.getAsString(Column_birth),value.getAsInteger(Column_headId));
			gamemdb.execSQL(InsertPlayerStr);
			this.getContext().getContentResolver().notifyChange(PLAYERINFO_URI, null);
		}else if(PEER_ID == URL_MATCHER.match(uri)){
			String InsertPeerStr = String.format("insert into %s(%s, %s, %s, %s, %s,%s) values('%s', %d, %d, '%s', '%d',%d)", 
					mtable_peer,Column_name,Column_jinder,Column_blood,Column_birth,Column_headId,Column_ID,
					value.getAsString(Column_name),value.getAsInteger(Column_jinder),value.getAsInteger(Column_blood),
					value.getAsString(Column_birth),value.getAsInteger(Column_headId),value.getAsInteger(Column_ID));
			gamemdb.execSQL(InsertPeerStr);
			this.getContext().getContentResolver().notifyChange(PEERINFO_URI, null);
		}else if(PLAYER_STATE == URL_MATCHER.match(uri)){
			String InsertPlayerState = String.format("insert into %s(%s,%s,%s,%s,%s,%s) values(%d,%d,%d,%d,%d, '%s')",mtable_player_state,
					Column_ID,Column_knowledge,
					Column_Hp,Column_feel,Column_Money,Column_Date,
					value.getAsInteger(Column_ID),
					value.getAsInteger(Column_knowledge),
					value.getAsInteger(Column_Hp),
					value.getAsInteger(Column_feel),
					value.getAsInteger(Column_Money),
					value.getAsString(Column_Date));
			gamemdb.execSQL(InsertPlayerState);
			this.getContext().getContentResolver().notifyChange(PLAYERINFO_STATE_URI, null);
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		Log.i(TAG,"Create provider");
		mdb_helper = new DatabaseHelper(this.getContext(),mdb_name,null,2);
		gamemdb = mdb_helper.getWritableDatabase();
		
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] colum, String selection, String[] selectionArgs,
			String sortOrder) {
		Cursor mCursor = null;
		
		if(PLAYER_ID == URL_MATCHER.match(uri)){
			try{
				mCursor = gamemdb.query(mtable_player,colum, selection, selectionArgs, null, null, sortOrder);
			}catch(Exception e){
				Log.i(TAG,""+e.getMessage());
			}
		}else if(PEER_ID == URL_MATCHER.match(uri)){
			mCursor = gamemdb.query(mtable_peer,colum, selection, selectionArgs, null, null, sortOrder);
			this.getContext().getContentResolver().notifyChange(PEERINFO_URI, null);
		}else if(PLAYER_STATE == URL_MATCHER.match(uri)){
			
			mCursor = gamemdb.query(mtable_player_state,colum, selection, selectionArgs, null, null, sortOrder);
		}
		if(null == mCursor){
			Log.i(TAG,"the tabel have no data");
		}
		
		return mCursor;
	}

	@Override
	public int update(Uri uri, ContentValues value, String selection, String[] args) {
		Cursor mCursor = null;
		
		if(PLAYER_ID == URL_MATCHER.match(uri)){
			try{
				gamemdb.update(mtable_player, value, selection, args);
				this.getContext().getContentResolver().notifyChange(PLAYERINFO_URI, null);
			}catch(Exception e){
				Log.i(TAG,""+e.getMessage());
			}
		}else if(PEER_ID == URL_MATCHER.match(uri)){
			gamemdb.update(mtable_peer, value, selection, args);
			
		}else if(PLAYER_STATE == URL_MATCHER.match(uri)){
			gamemdb.update(mtable_player_state, value, selection, args);
			this.getContext().getContentResolver().notifyChange(PLAYERINFO_STATE_URI, null);
		}
		return 0;
	}
	public String getPlayerTableString(){
		return CreateTable_player;
	}
	public String getPeerTableString(){
		return CreateTable_peer;
	}
	public void setPlayerTableString(String sql){
		 CreateTable_player = sql;
	}
	public void setPeerTableString(String sql){
		 CreateTable_peer = sql;
	}
}
